package com.ruoyi.system.ali;

import cn.hutool.core.net.URLDecoder;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.enums.ChannelEnum;
import com.ruoyi.common.utils.file.AliOss;
import com.ruoyi.common.utils.sign.SignMD5Util;
import com.ruoyi.system.biz.CashBizService;
import com.ruoyi.system.domain.Bank;
import com.ruoyi.system.domain.CashHistory;
import com.ruoyi.system.domain.Order;
import com.ruoyi.system.dto.ApiByPhoneAddDto;
import com.ruoyi.system.service.BankService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ZfbConfigService;
import com.ruoyi.system.vo.ApiPayResultVo;

import javax.annotation.Resource;
import java.nio.charset.Charset;
import java.util.TreeMap;

/**
 * uid
 */
//@Component
public class UidPay1Service implements ChannelService {
    @Resource
    ISysConfigService configService;

    @Resource
    private ZfbConfigService zfbConfigService;
    @Resource
    private BankService bankService;

    @Override
    public String getType() {
        return ChannelEnum.UID.getCode();
    }

    @Resource
    private CashBizService cashService;

    @Override
    public Object create(AliPayDto aliPayDto, Order order) {
        Bank bank = bankService.getRand(order.getAmount(), getType());
        return create(order, bank);
    }

    @Override
    public Object createByPhone(ApiByPhoneAddDto apiByPhoneAddDto, Order order) {
        Bank bank = bankService.getUserBank(order.getUserId(), order.getAmount(), getType());
        return create(order, bank);
    }

    //uid
    public Object create(Order order, Bank bank) {

        //获取一个支付宝的uid
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("s", "money");
        jsonObject.put("u", bank.getZfbUserId());
//        jsonObject.put("u", "2323232323");
        jsonObject.put("a", order.getAmount());
        jsonObject.put("m", "温馨提示: 为减少风控 ,请尽量停顿30秒以上付款");
        String decode = URLDecoder.decode(jsonObject.toJSONString(), Charset.defaultCharset());

        String str = "https://ds.alipay.com/?from=pc&appId=20000116&actionType=toAccount&goBack=NO&amount=30.00&userId=" + bank.getUserId() + "&memo=2023120323071982831";
        //将链接解析成二维码
        String qrCode = "alipays://platformapi/startapp?appId=20000123&actionType=scan&biz_data=" + decode;
        QrConfig qrConfig = new QrConfig();
        qrConfig.setWidth(200);
        qrConfig.setHeight(200);
        byte[] bytes = QrCodeUtil.generatePng(qrCode, qrConfig);
        String projectName = configService.selectConfigByKey("projectName");
        String picUrl = AliOss.uploadToOss(bytes, projectName + "/orderReceive/" + order.getOrderNo() + ".jpg");
//        Map<String, Object> map = new HashMap<>();
//        //移动端支付宝跳转链接
//        map.put("url", qrCode);
//        map.put("pic", picUrl);
//        map.put("amount", order.getAmount());
//        map.put("orderNo", order.getOrderNo());
        AliPayVo aliPayVo = new AliPayVo();
        aliPayVo.setOrderNo(order.getOrderNo());
        aliPayVo.setAmount(order.getAmount());
        aliPayVo.setPic(picUrl);
        aliPayVo.setQrCode(qrCode);
        //冻结收款用户的金额
        CashHistory userCashHis = new CashHistory(bank.getUserId(), order.getAmount(), "reduce", "代收订单", "freeze");
        userCashHis.setOrderNo(order.getOrderNo());
        cashService.updateCash(bank.getUserId(), userCashHis);

        order.setAccount(bank.getCardNo());
        order.setName(bank.getName());
        order.setChannelName(ChannelEnum.UID.getName());
        order.setType("hand");
        order.setBankName("支付宝");
        order.setPic(picUrl);
        order.setZfbUrl(qrCode);
        order.setUserId(bank.getUserId());
        order.setUsername(bank.getUsername());

        ApiPayResultVo apiVo = new ApiPayResultVo();
        apiVo.setAmount(String.valueOf(order.getAmount()));
        apiVo.setOrderNo(order.getOrderNo());
        apiVo.setMerchantId(order.getMerchantId());
        TreeMap<String, String> map = SignMD5Util.objToMap(apiVo);
        String sign = SignMD5Util.getSign(map, "sign");
        String payUrl = configService.selectConfigByKey("payUrl");
        payUrl = payUrl + "?" + SignMD5Util.getStr(map, sign);
        apiVo.setUrl(payUrl);
        return apiVo;
    }


}
